package jjn.round1;

/**
 * @author Jjn
 * @since 2022/9/9 23:45
 */
public class LeetCode200_NumberOfIslands {
    public int numIslands(char[][] grid) {
        int height = grid.length;
        int width = grid[0].length;
        int num = 0;
        for (int j = 0; j < height; j++) {
            for (int i = 0; i < width; i++) {
                if (grid[j][i] == '0') {
                    continue;
                }
                dfs(grid, i, j);
                num++;
            }
        }
        return num;
    }

    private void dfs(char[][] grid, int i, int j) {
        int height = grid.length;
        int width = grid[0].length;
        if (i < 0 || i >= width || j < 0 || j >= height || grid[j][i] == '0') {
            return;
        }
        grid[j][i] = '0';
        dfs(grid, i + 1, j);
        dfs(grid, i - 1, j);
        dfs(grid, i, j + 1);
        dfs(grid, i, j - 1);
    }
}
